package net.shopxx.action.shop;

import java.math.BigDecimal;
import java.net.URL;
import java.text.ParseException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;

import net.shopxx.entity.AirCompany;
import net.shopxx.entity.AirOrder;
import net.shopxx.entity.AirOrder.OrderStatus;
import net.shopxx.entity.AirOrder.PaymentStatus;
import net.shopxx.service.AirCompanyService;
import net.shopxx.service.AirOrderService;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.axiyue.trip.request.Body;
import com.axiyue.trip.request.Flight;
import com.axiyue.trip.request.InsuranceInfo;
import com.axiyue.trip.request.LinkerInfo;
import com.axiyue.trip.request.OrderInfo;
import com.axiyue.trip.request.Passenger;
import com.axiyue.trip.request.Price;
import com.axiyue.trip.request.TripRequest;
import com.axiyue.trip.response.CitySpecTicket;
import com.axiyue.trip.response.TripResponse;
import com.axiyue.trip.response.TripUtil;

/**
 * 后台Action类 - 商品评论
 */

@ParentPackage("shop")
public class TripAction extends BaseShopAction {

	private TripResponse res;
	private TripRequest req;
	private Flight fromFlight;
	private Flight toFlight;
	private String tickeType="s";
	private String fromCityName;
	private String toCityName;
	private String cabinClass;
	private String carrier;
	//航空公司列表
	private List<AirCompany> airCompanyList;
	//
	private List<CitySpecTicket> citySpecTicketList;
	
	private List<Passenger> passenger;

	@Resource
	private AirCompanyService airCompanyService;
	
	@Resource
	private AirOrderService airOrderService;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private Price price;
	private OrderInfo orderInfo;
	private LinkerInfo linkerInfo;
	private InsuranceInfo insuranceInfo;
	
	

	// 列表
	public String index() {
		airCompanyList = airCompanyService.getAll();
		return "index";
	}

	// 列表
	@SuppressWarnings("deprecation")
	public String showfare() throws Exception {
		if(tickeType==null)
		{
			return index();
		}
		TripRequest request = TripUtil
				.getRequestInstance(TripUtil.REQUEST_TYPE.SINGLE_TRIP_FLIGHT_PRICE);
		Body body = request.getBody();
		body.setOa(fromFlight.getFromCity());
		body.setAa(fromFlight.getArriveCity());
		body.setFDate(fromFlight.getDepartureDate());
		body.setCabinClass(cabinClass);

		String reqStr = TripUtil.gernateXml(request);
		System.out.println(reqStr);
//		reqStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
//		"<request><header><accountID>xysc01</accountID><digitalSign>F74F684FEC21E74C</digitalSign><gzip>0</gzip>" +
//		"<serviceName>getFlightPrice</serviceName></header>"
//		+"<body><oa>SHA</oa><aa>PEK</aa><fDate>2012-11-21</fDate></body></request>";
//		System.out.println("req:"+reqStr);
		String responseStr = connecteNet(new URL(airURI), reqStr);
		res = TripUtil.getTicketInfoStr(responseStr);
//		TripRequest specPriceReq = TripUtil
//				.getRequestInstance(TripUtil.REQUEST_TYPE.SPEC_FLIGTH_PRICE);
//		Body specBody = specPriceReq.getBody();
//		specBody.setOa(fromFlight.getFromCity());
//		specBody.setAa(fromFlight.getArriveCity());
//		specBody.setDate(fromFlight.getDepartureDate());
//		specBody.setDays(7);
//		reqStr = TripUtil.gernateXml(specPriceReq);
//		System.out.println("req:"+reqStr);
		 reqStr =String.format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><request>" +
				 "<header><accountID>bycxysc01</accountID><digitalSign>F74F684FEC21E74C</digitalSign><gzip>0</gzip>"
				 + "<serviceName>getSpecFlightPrice</serviceName></header><body><oa>%s</oa><aa>%s</aa>"
				 + "<date>%s</date><days>%s</days></body></request>", fromFlight.getFromCity(),fromFlight.getArriveCity(),TripUtil.recalculator(fromFlight.getDepartureDate(), 8),8);
		responseStr = connecteNet(new URL(airURI), reqStr);
		System.out.println("www:"+responseStr);
		TripResponse specTicket = TripUtil.getTicketInfoStr(responseStr);
//		TripResponse specTicket = TripUtil.getSpecTicket();
		res.getBody().setSpecTicket(specTicket.getBody().getSpecTicket());
		int startDay = 0;
		try {
			if (!res.getBody().getSpecTicket().isEmpty()) {
				startDay = TripUtil.DF_YYYY_MM_DD.parse(
						res.getBody().getSpecTicket().get(0).getDate())
						.getDay();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		getRequest().setAttribute("startId", startDay);

		return "showfare";
	}

	// 列表
	@SuppressWarnings("deprecation")
	public String showback() throws Exception {
		if(tickeType==null)
		{
			return index();
		}
		getSession().put("fromFlight", fromFlight);
		TripRequest request = TripUtil
				.getRequestInstance(TripUtil.REQUEST_TYPE.SINGLE_TRIP_FLIGHT_PRICE);
		Body body = request.getBody();
		body.setOa(fromFlight.getArriveCity());
		body.setAa(fromFlight.getFromCity());
		body.setFDate(toFlight.getDepartureDate());
		body.setCabinClass(cabinClass);

		String reqStr = TripUtil.gernateXml(request);
		String responseStr = connecteNet(new URL(airURI), reqStr);

		res = TripUtil.getTicketInfoStr(responseStr);
		
		reqStr =String.format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><request>" +
				 "<header><accountID>bycxysc01</accountID><digitalSign>F74F684FEC21E74C</digitalSign><gzip>0</gzip>"
				 + "<serviceName>getSpecFlightPrice</serviceName></header><body><oa>%s</oa><aa>%s</aa>"
				 + "<date>%s</date><days>%s</days></body></request>", fromFlight.getArriveCity(),fromFlight.getFromCity(),TripUtil.recalculator(toFlight.getDepartureDate(), 8),8);
		
		System.out.println(reqStr);
		
		responseStr = connecteNet(new URL(airURI), reqStr);
		System.out.println("www:"+responseStr);
		
		
		responseStr = connecteNet(new URL(airURI), reqStr);
		TripResponse specTicket = TripUtil.getTicketInfoStr(responseStr);
		res.getBody().setSpecTicket(specTicket.getBody().getSpecTicket());
		int startDay = 0;
		try {
			if (!res.getBody().getSpecTicket().isEmpty()) {
				startDay = TripUtil.DF_YYYY_MM_DD.parse(
						res.getBody().getSpecTicket().get(0).getDate())
						.getDay();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		getRequest().setAttribute("startId", startDay);

		return "showback";
	}

	@SuppressWarnings("deprecation")
	public String subinfo() {
		if(tickeType==null)
		{
			return index();
		}
		if (tickeType.equalsIgnoreCase("m"))
			getSession().put("toFlight", toFlight);
		else
			getSession().put("fromFlight", fromFlight);
		return "subinfo";
	}

	@SuppressWarnings("deprecation")
	public String completed() {
		for (Passenger pass : passenger) {
			System.out.println("name:"+pass.getName());
		}
		return "completed";
	}

	@SuppressWarnings("deprecation")
	public String pays() throws Exception {
		fromFlight= (Flight) getSession().get("fromFlight");
		int personNum = passenger.size();
		int banlance = 1;
		int seat = personNum * banlance;

		TripRequest request = TripUtil
				.getRequestInstance(TripUtil.REQUEST_TYPE.SUBMIT_ORDER);
		Body body = request.getBody();
		body.setOa(fromFlight.getFromCity());
		body.setAa(fromFlight.getArriveCity());
		body.setFDate(fromFlight.getDepartureDate());
		body.setCabinClass(cabinClass);
		
		body.getOverflight().getFlights().addFlight(fromFlight);
		body.getOverflight().getPassengers().setPassenger(passenger);
		body.getOverflight().setInsuranceInfo(insuranceInfo);
		body.getOverflight().setOrderInfo(orderInfo);
//		body.getOverflight().getPrices().setPrice(price);
		
		AirOrder airOrder = new AirOrder();
		net.shopxx.entity.LinkerInfo linkerInfoDao = new net.shopxx.entity.LinkerInfo();
		net.shopxx.entity.Flight fromFlightDao = new net.shopxx.entity.Flight();
		net.shopxx.entity.Flight toFlightDao = new net.shopxx.entity.Flight();
		net.shopxx.entity.OrderInfo orderInfoDao = new net.shopxx.entity.OrderInfo();
		net.shopxx.entity.Price price = new net.shopxx.entity.Price();
		
		Set<net.shopxx.entity.Flight> flightSet = new HashSet<net.shopxx.entity.Flight>();
		flightSet.add(fromFlightDao);

		airOrder.setLinkerInfo(linkerInfoDao);
		airOrder.setOrderInfo(orderInfoDao);

		Set<net.shopxx.entity.Price> priceSet = new HashSet<net.shopxx.entity.Price>();
		price.setFare(11f);
		price.setAirportTax(150f);
		price.setPriceID("0");
		price.setAirOrder(airOrder);
		priceSet.add(price);
		airOrder.setPriceSet(priceSet);
		BeanUtils.copyProperties(linkerInfoDao, linkerInfo);
		BeanUtils.copyProperties(fromFlightDao, fromFlight);
		fromFlightDao.setAirOrder(airOrder);
//			BeanUtils.copyProperties(orderInfoDao, orderInfo);
		if (tickeType.equalsIgnoreCase("m"))
		{
			toFlight= (Flight) getSession().get("toFlight");
			body.getOverflight().getFlights().addFlight(toFlight);
			BeanUtils.copyProperties(toFlightDao, toFlight);
			toFlightDao.setAirOrder(airOrder);
			flightSet.add(toFlightDao);
		}
			
		airOrder.setFlightSet(flightSet);
		price.setAirportTax(1f);
		
		Set<net.shopxx.entity.Passenger> passengerSet = new HashSet<net.shopxx.entity.Passenger>();
		for (Passenger pass : passenger) {
			net.shopxx.entity.Passenger passDao = new net.shopxx.entity.Passenger();
			BeanUtils.copyProperties(passDao, pass);
			passDao.setAirOrder(airOrder);
			passengerSet.add(passDao);
		}
		airOrder.setPassengerSet(passengerSet);
		airOrder.setOrderStatus(OrderStatus.unprocessed);
		airOrder.setOrderSn(UUID.randomUUID().toString());
		airOrder.setDeliveryFee(new BigDecimal(5));
		airOrder.setTotalAmount(new BigDecimal(125));
		airOrder.setPaymentFee(new BigDecimal(0));
		airOrder.setDeliveryTypeName("申通快递");
		airOrder.setPaymentConfigName("s");
		airOrder.setPaymentStatus(PaymentStatus.unpaid);
		airOrder.setProductTotalPrice(new BigDecimal(200));
		//submit sendOrder;
		String reqStr = TripUtil.gernateXml(request);
		//reponse order
		String responseStr = connecteNet(new URL(airURI), reqStr);
		TripResponse specTicket = TripUtil.getTicketInfoStr(responseStr);
		if(specTicket != null)
		{
			airOrderService.save(airOrder);
		}
		
		TripRequest requestCheckCabin = TripUtil
		.getRequestInstance(TripUtil.REQUEST_TYPE.CHECKCABIN);
		
		boolean isHave = true;
		
		Body checkBody = requestCheckCabin.getBody();
		checkBody.setFNo(fromFlight.getFlightNo());
		checkBody.setDate(fromFlight.getDepartureDate());
		checkBody.setCode("M");
		checkBody.setSeat(""+seat);
		reqStr = TripUtil.gernateXml(requestCheckCabin);
		responseStr = connecteNet(new URL(airURI), reqStr);
		TripResponse checkRes = TripUtil.getTicketInfoStr(responseStr);
		//hasSeat =false
		if(checkRes != null)
		{
			isHave = false;
			//ret
		}
		
		if (tickeType.equalsIgnoreCase("m"))
		{
			Body checkBodyBack = requestCheckCabin.getBody();
			checkBodyBack.setFNo(toFlight.getFlightNo());
			checkBodyBack.setDate(toFlight.getDepartureDate());
			checkBodyBack.setCode("M");
			checkBodyBack.setSeat(""+seat);
			reqStr = TripUtil.gernateXml(requestCheckCabin);
			responseStr = connecteNet(new URL(airURI), reqStr);
			TripResponse checkResBack = TripUtil.getTicketInfoStr(responseStr);
			//hasSeat =false
			if(checkResBack != null)
			{
				isHave = false;
				//ret
			}
		}
		
		if(!isHave)
		{
			//ret
		}
		//checkFlightCabin
		//checkFlightCabinResponse
		
		
		return "pays";
	}

	// 列表
	public String list() {
		return LIST;
	}

	public TripResponse getRes() {
		return res;
	}

	public void setRes(TripResponse res) {
		this.res = res;
	}

	public TripRequest getReq() {
		return req;
	}

	public void setReq(TripRequest req) {
		this.req = req;
	}

	public Flight getFromFlight() {
		return fromFlight;
	}

	public void setFromFlight(Flight fromFlight) {
		this.fromFlight = fromFlight;
	}

	public Flight getToFlight() {
		return toFlight;
	}

	public void setToFlight(Flight toFlight) {
		this.toFlight = toFlight;
	}

	public String getTickeType() {
		return tickeType;
	}

	public void setTickeType(String tickeType) {
		this.tickeType = tickeType;
	}

	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}

	public String getCabinClass() {
		return cabinClass;
	}

	public void setCabinClass(String cabinClass) {
		this.cabinClass = cabinClass;
	}

	public String getCarrier() {
		return carrier;
	}

	public void setCarrier(String carrier) {
		this.carrier = carrier;
	}

	public List<AirCompany> getAirCompanyList() {
		return airCompanyList;
	}

	public void setAirCompanyList(List<AirCompany> airCompanyList) {
		this.airCompanyList = airCompanyList;
	}

	public List<Passenger> getPassenger() {
		return passenger;
	}

	public void setPassenger(List<Passenger> passenger) {
		this.passenger = passenger;
	}

	public Price getPrice() {
		return price;
	}

	public void setPrice(Price price) {
		this.price = price;
	}

	public OrderInfo getOrderInfo() {
		return orderInfo;
	}

	public void setOrderInfo(OrderInfo orderInfo) {
		this.orderInfo = orderInfo;
	}

	public LinkerInfo getLinkerInfo() {
		return linkerInfo;
	}

	public void setLinkerInfo(LinkerInfo linkerInfo) {
		this.linkerInfo = linkerInfo;
	}

	public InsuranceInfo getInsuranceInfo() {
		return insuranceInfo;
	}

	public void setInsuranceInfo(InsuranceInfo insuranceInfo) {
		this.insuranceInfo = insuranceInfo;
	}

	public List<CitySpecTicket> getCitySpecTicketList() {
		return citySpecTicketList;
	}

	public void setCitySpecTicketList(List<CitySpecTicket> citySpecTicketList) {
		this.citySpecTicketList = citySpecTicketList;
	}
	
	

}